﻿using MathMLToCSharp;
using MathMLToCSharp.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace SMART_VIET_V01
{
    public partial class testFrm : Form
    {
        public testFrm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            result.Clear();
            string text = dksMathEditor1.textResult();
            text = text.Replace("<mtable><mtr><mtd>", "");
            text = text.Replace("</mtd></mtr></mtable>", "");
            //result.AppendText(ParseAndOutput(text));
            string mathML = text;
            mathML = mathML.Replace("&", "");
            mathML = mathML.Replace(";", "");
            mathML = mathML.Replace("plus", "+");
            mathML = mathML.Replace("minus", "-");
            mathML = mathML.Replace("times", "*");
            mathML = mathML.Replace("sol", "/");
            mathML = mathML.Replace("lbrace", "{");
            mathML = mathML.Replace("rbrace", "}");
            mathML = mathML.Replace("lpar", "(");
            mathML = mathML.Replace("rpar", ")");
            mathML = mathML.Replace("verbar", "|");
            mathML = mathML.Replace("lbrack", "[");
            mathML = mathML.Replace("rbrack", "]");
            mathML = mathML.Replace("excl", "!");
            mathML = mathML.Replace("sim", "~");
            mathML = mathML.Replace("equals", "=");
            mathML = mathML.Replace("amp", "&");
            mathML = mathML.Replace("num", "#");
            mathML = mathML.Replace("comma", ",");
            mathML = mathML.Replace("af", "&#8289;");
            result.AppendText(mathML);
        }
        private static string ParseAndOutput(string mathML)
        {
            return ParseAndOutput(mathML, new BuildContextOptions());
        }

        private static string ParseAndOutput(string mathML, BuildContextOptions options)
        {
            mathML = mathML.Replace("&", "");
            mathML = mathML.Replace(";", "");
            mathML = mathML.Replace("plus", "+");
            mathML = mathML.Replace("minus", "-");
            mathML = mathML.Replace("times", "*");
            mathML = mathML.Replace("sol", "/");
            mathML = mathML.Replace("lbrace", "{");
            mathML = mathML.Replace("rbrace", "}");
            mathML = mathML.Replace("lpar", "(");
            mathML = mathML.Replace("rpar", ")");
            mathML = mathML.Replace("verbar", "|");
            mathML = mathML.Replace("lbrack", "[");
            mathML = mathML.Replace("rbrack", "]");
            mathML = mathML.Replace("excl", "!");
            mathML = mathML.Replace("sim", "~");
            mathML = mathML.Replace("equals", "=");
            mathML = mathML.Replace("amp", "&");
            mathML = mathML.Replace("num", "#");
            mathML = mathML.Replace("comma", ",");
            mathML = mathML.Replace("af", "&#8289;");
            IBuildable b = Parser.Parse(XElement.Parse(mathML));
            StringBuilder sb = new StringBuilder();
            if (b != null)
            {
                b.Visit(sb, new BuildContext(options));
            }
            return sb.ToString();
        }

        private void dksMathEditor1_ViewCodeClick(object sender, EventArgs e)
        {
            MessageBox.Show("cl");
        }
    }
}
